package com.haloer.net
{
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	public class HTTP
	{
		private var _loader :URLLoader;
		private var _method :String;
		public var url :String;
		public var data :Object;
		public var onComplete :Function;
		public var onError :Function;
		
		public function HTTP(dataFormat:String = "text", method:String = "get")
		{
			_loader = new URLLoader();
			_loader.dataFormat = dataFormat;
			_method = method;
		}
		
		public function load(loadUrl:String = "") : void
		{
			var urlVars:URLVariables = null;
			var key:String = null;
			if (loadUrl == "" && url == "")
			{
				throw new Error("无效的url！");
			}
			if (loadUrl)
			{
				url = loadUrl;
			}
			addEvent();
			var urlReq :URLRequest = new URLRequest(url);
			urlReq.method = _method;
			if (data)
			{
				urlVars = new URLVariables();
				for (key in data)
				{
					
					urlVars[key] = data[key];
				}
				urlReq.data = urlVars;
			}
			
			_loader.load(urlReq);
		}
		
		private function addEvent() : void
		{
			_loader.addEventListener(Event.COMPLETE, complete);
			_loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_loader.addEventListener(Event.OPEN, openHandler);
			_loader.addEventListener(ProgressEvent.PROGRESS, progressHandler);
		}// end function
		
		private function removeEvent() : void
		{
			_loader.removeEventListener(Event.COMPLETE, complete);
			_loader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatus);
			_loader.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_loader.removeEventListener(Event.OPEN, openHandler);
			_loader.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
		}// end function
		
		private function progressHandler(event:ProgressEvent) : void
		{
		}// end function
		
		private function openHandler(event:Event) : void
		{
		}// end function
		
		private function complete(event:Event) : void
		{
			removeEvent();
			if (onComplete is Function)
			{
				onComplete(event.target.data);
			}
		}// end function
		
		private function httpStatus(event:HTTPStatusEvent) : void
		{
		}// end function
		
		private function ioErrorHandler(event:IOErrorEvent) : void
		{
			removeEvent();
			if (onError is Function)
			{
				onError();
			}
		}// end function
		
		private function securityErrorHandler(event:SecurityErrorEvent) : void
		{
			removeEvent();
			if (onError is Function)
			{
				onError();
			}
		}// end function
		
		public function get loader() : URLLoader
		{
			return _loader;
		}
	}
}